﻿#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include "Windows.h"
#pragma pack(1)
const int TDXLINE_HEAD_SKIP = 0xa56 - 44;
#define FIELD_OFFSET(type, field)    ((LONG)&(((type *)0)->field)) 
typedef struct {
	char head[44];
}tdxline_dat_head;
typedef struct {
	unsigned int date;
	float price;
	unsigned int  unknown;
}tdxline_point;
typedef struct {
	char market;
	char name[7];
	char rev[16];
	char type1;
	char type2[61];
	char unknown[13];
	unsigned short peroid;//11年，10季，6月，5周，4日，3-60分，2-30分，1-15分，0-5分，7-1分
	unsigned int dummy;
	tdxline_point point[2];
	char rev2[88];
	char rgb[4];
	char rev3[157];
}tdxline_item;

int read_dat(const char* file_name);

const char* tdxline = "E:\\hubin\\st\\new_gxzq_v6-1\\T0002\\tdxline.dat";

int main(int argc, char* argv[])
{
	read_dat(tdxline);
}


int read_dat(const char* file_name)
{
	FILE* fp;
	if ((fp = fopen(file_name, "rb")) == NULL)
	{
		printf("Error: Can't open :%s! ", file_name);
		return -1;
	}
	int ndata = 0;

	tdxline_dat_head head;
         	fread(&head, sizeof(tdxline_dat_head), 1, fp);
	fseek(fp, TDXLINE_HEAD_SKIP, SEEK_CUR);

	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	tdxline_item item;
	int n = 1;
	printf("\n size:%d", sizeof(tdxline_item));
	int offset_rgb = FIELD_OFFSET(tdxline_item, rgb);
	int offset_date1 = FIELD_OFFSET(tdxline_item, point[0].date);
	int offset_date2 = FIELD_OFFSET(tdxline_item, point[1].date);
#if 1
	int offset =sizeof(tdxline_item);
	offset = -offset;
	fseek(fp, offset, SEEK_END);
#endif
	while (fread(&item, sizeof(tdxline_item), 1, fp) > 0) {
		printf("\n%d %s %d %d %f   %d    %f %x", n++, item.name, item.peroid, item.point[0].date,item.point[0].price, item.point[1].date, item.point[1].price,item.rgb);
		char* p =(char *) &item;

		for (int i = 0; i < sizeof(tdxline_item); i++, p++) {
			if (i % 16 == 0) {
				printf("\n");
			}
			if ((i >= offset_rgb && i<= offset_rgb + 3) || (i >= offset_date1 && i <= offset_date1 + 3)|| (i >= offset_date2 && i <= offset_date2 + 3)) {
				SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
			}
			else {
				SetConsoleTextAttribute(handle, 0x07);
			}
			printf("%02x ", (unsigned char)*p);
		}
		_getch();
	}

	fclose(fp);

	if (_getch() == 0) _getch();
	return 0;
}
